﻿
using HMaker.Utils;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace HMaker.Controls
{
    public class ShapeImage : ShapeControl
    {
        #region 内部变量
        /// <summary>
        /// 记录要绘制的图形
        /// </summary>
        private Bitmap _Image = null;

        /// <summary>
        /// 指示图片是否在加载后被修改过
        /// </summary>
        public bool IsChanged { get; private set; }

        /// <summary>
        /// 当已经被选择时是否应该使用色块代替
        /// </summary>
        public bool HighlightWhenSelected { get; set; }

        /// <summary>
        /// 记录从图形左上角到targetPoint的偏移
        /// </summary>
        private Point _targetOffset = Point.Empty;

        private string _ImgTypeName = "";
        private string _ImgIdentifyName = "";

        /// <summary>
        /// 读取或设置图片的类型名称
        /// </summary>
        public string ImgTypeName
        {
            get => _ImgTypeName;
            set
            {
                _ImgTypeName = value;
                IsChanged = true;
            }
        }

        /// <summary>
        /// 读取或设置图片的可读名称
        /// </summary>
        public string ImgIdentifyName
        {
            get => _ImgIdentifyName;
            set
            {
                _ImgIdentifyName = value;
                IsChanged = true;
            }
        }

        /// <summary>
        /// 给定一个偏移,生成此图片的名称
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public string GetName(Point offset)
        {
            return $"{ImgTypeName}${ImgIdentifyName}${offset.X},{offset.Y}.bmp";
        }

        /// <summary>
        /// 保存图片到文件中
        /// </summary>
        /// <param name="path"></param>
        public void SaveFile(string path)
        {
            if (Image == null)
            {
                throw new ArgumentNullException("图片为NULL");
            }

            if (IsChanged)
            {
                Bitmap oldBitmap = Image;
                Image = new Bitmap(oldBitmap).Clone(new Rectangle(Point.Empty, oldBitmap.Size), PixelFormat.Format24bppRgb); ;
                oldBitmap.Dispose();
                Image.Save(path, ImageFormat.Bmp);
                IsChanged = false;
            }
        }

        public override Point CenterPos
        {
            get => Location.Add(_targetOffset);
            set => Location = value.Sub(_targetOffset);
        }

        #endregion

        #region 对外接口

        /// <summary>
        /// 重写是否允许修改大小默认为false
        /// </summary>
        public override bool CanReszie { get; set; } = false;

        public ShapeImage(Point targetPoint, Point offset, Bitmap image, OriginPos origin) : base(origin)
        {
            _targetOffset = offset;
            CenterPos = targetPoint;
            _Image = image ?? throw new NullReferenceException("图片不能为null");
            Size = _Image.Size;
            IsChanged = true;
        }

        public ShapeImage(Point targetPoint, Point offset, string imagePath, OriginPos origin) : base(origin)
        {
            _targetOffset = offset;
            CenterPos = targetPoint;
            _Image = new Bitmap(imagePath) ?? throw new NullReferenceException("图片不能为null");
            Size = _Image.Size;
            IsChanged = false;
        }

        /// <summary>
        /// 读取或设置当前图形保存的图片
        /// </summary>
        public Bitmap Image
        {
            get => _Image;
            set
            {
                if (_Image != value)
                {
                    IsChanged = true;
                    Size = value?.Size ?? new Size(16, 16);
                    _Image = value;
                    Invalidate();
                }
            }
        }

        public override ShapeControl Clone()
        {
            return new ShapeImage(CenterPos, _targetOffset, Image, OriginPos);
        }

        #endregion

        protected override GraphicsPath OnDrawRegion()
        {
            return null;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (HighlightWhenSelected && IsSelect)
            {
                e.Graphics.FillRectangle(Brushes.LightBlue, 0, 0, _Image?.Width ?? 16, _Image?.Height ?? 16);
            }
            else
            {
                if (_Image == null)
                {
                    e.Graphics.DrawIcon(SystemIcons.Error, new Rectangle(Point.Empty, new Size(16, 16)));
                }
                else
                {
                    e.Graphics.DrawImage(_Image, 0, 0, _Image.Width, _Image.Height);
                }
            }
            base.OnPaint(e);
        }
    }
}
